import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/widgets/bottom_sheet/apply_credit_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/delete_info_bottom_sheet.dart';
import 'package:flutter/material.dart';

mixin ApplyCreditMixin<T> on BaseProvider<T> {
  String creditValue;
  String currencyName;
  num availableCredit = 0;

  double get total;

  bool isBill = false;

  double get creditAmount {
    if (creditValue == null) return 0.0;
    double credit = 0.0;

    credit = double.parse(creditValue, (e) => 0.0);

    return credit;
  }

  double get creditUsed {
    if (creditValue == null) return null;
    double creditAmount = 0.0;

    creditAmount = double.parse(creditValue, (e) => 0.0);

    return creditAmount;
  }

  void onAddCredit() {
    obtainContext?.call((context) async {
      final result = await showApplyCreditBottomSheet(
        context,
        currencyName,
        isBill: isBill,
        availableCredit: availableCredit,
        totalAmount: total,
        creditValue: creditValue,
      );
      if (result != null) {
        if (result is bool) {
          creditValue = null;
        } else {
          creditValue = result;
        }
      }
      update();
    });
  }

  void onAskApplyCredit({@required Function onContinue}) {
    obtainContext?.call((context) async {
      showApplyCreditInfoBottomSheet(context,
          '${S.current.credit_} $currencyName ${availableCredit.toStringAsFixed(2)}',
          isBill: isBill, onDelete: () async {
        Navigator.pop(context);
        onAddCredit();
      }, onCancel: () {
        Navigator.pop(context);
        onContinue();
      });
    });
  }
}
